package com.example.weather

import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.activity.ComponentActivity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weather.weather.Forecast;
import com.example.weather.weather.Weather;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


class Main2Activity : AppCompatActivity() {

    // 定义基础的URL
    val baseURL = "http://t.weather.sojson.com/api/weather/city/";

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            insets
        };

        val cityCode = intent.getStringExtra("city_code");
        val queue: RequestQueue = Volley.newRequestQueue(this);
        val stringRequest = StringRequest(baseURL + cityCode, {
            val gson = Gson();
            // 创建正确的TypeToken用于解析Weather对象
            val WeatherType = object : TypeToken<Weather>() {}.type;
            val weather = gson.fromJson<Weather>(it, WeatherType);
            val textView_city = findViewById<TextView>(R.id.textView_city);
            textView_city.text = weather.cityInfo.city;
            val textView_province = findViewById<TextView>(R.id.textView_province);
            textView_province.text = weather.cityInfo.parent;
            val textView_shidu = findViewById<TextView>(R.id.textView_shidu);
            textView_shidu.text = weather.data.shidu;
            val textView_wendu = findViewById<TextView>(R.id.textView_wendu);
            textView_wendu.text = weather.data.wendu;
            // 获取预报数据中的第一天数据
            val firstDay: Forecast = weather.data.forecast[0];
            val imageView = findViewById<ImageView>(R.id.imageView);
            when (firstDay.type) {
                "晴" -> imageView.setImageResource(R.drawable.sun);
                "阴" -> imageView.setImageResource(R.drawable.cloud);
                "多云" -> imageView.setImageResource(R.drawable.mcloud);
                "阵雨" -> imageView.setImageResource(R.drawable.rain);
                else -> imageView.setImageResource(R.drawable.thunder);
            }
            // 创建ArrayAdapter时不需要带泛型<Forecast>，因为已经知道是Forecast类型的数组
            val adapter = ArrayAdapter<Forecast>(this, android.R.layout.simple_list_item_1, weather.data.forecast);
            val listView = findViewById<ListView>(R.id.listView);
            listView.adapter = adapter;


            Log.d( "Main2Activity",  "${weather.cityInfo.city} ${weather.cityInfo.parent}")
        }, {
            Log.d( "Main2Activity", "$it")
        })
        queue.add(stringRequest)
    }

}